
package com.coolgor.coldot.service.stock.impl;

import com.coolgor.coldot.common.PageCalculator;
import com.coolgor.coldot.entity.stock.StockSpecialDay;
import com.coolgor.coldot.dao.stock.StockSpecialDayDao;
import com.coolgor.coldot.dto.stock.StockSpecialDayExecution;
import com.coolgor.coldot.enums.stock.StockSpecialDayOperationStateEnum;
import com.coolgor.coldot.exceptions.stock.StockSpecialDayOperationException;
import com.coolgor.coldot.service.stock.StockSpecialDayService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;
import java.util.ArrayList;


@Service
public class StockSpecialDayServiceImpl implements StockSpecialDayService {

	private static Logger log = LoggerFactory.getLogger(StockSpecialDayServiceImpl.class);

	@Autowired
	StockSpecialDayDao stockSpecialDayDao;

	@Override
	public StockSpecialDayExecution getByID(Long recordID) {
		// 空值判断
		if (null == recordID || recordID.equals("")) {
			return new StockSpecialDayExecution(StockSpecialDayOperationStateEnum.NULL_ID);
		}
		StockSpecialDay stockSpecialDay = stockSpecialDayDao.queryByID(recordID);
		if (stockSpecialDay != null) {
			return new StockSpecialDayExecution(StockSpecialDayOperationStateEnum.SUCCESS, stockSpecialDay);
		} else {
			return new StockSpecialDayExecution(StockSpecialDayOperationStateEnum.NULL);
		}
	}

	@Override
	public StockSpecialDayExecution getList(StockSpecialDay stockSpecialDayCondition, int pageIndex, int pageSize) {
		// 将页码转换成行码
		int rowIndex = PageCalculator.calculateRowIndex(pageIndex, pageSize);
		// 依据查询条件，调用dao层返回相关的查询结果列表
		List<StockSpecialDay> list = stockSpecialDayDao.queryList(stockSpecialDayCondition, rowIndex, pageSize);
		// 依据相同的查询条件，返回查询结果总数
		int count = stockSpecialDayDao.queryCount(stockSpecialDayCondition);
		StockSpecialDayExecution result = new StockSpecialDayExecution();
		if (list != null) {
			result.setState(StockSpecialDayOperationStateEnum.SUCCESS.getState());
			result.setStateInfo(StockSpecialDayOperationStateEnum.SUCCESS.getStateInfo());
			result.setStockSpecialDayList(list);
			result.setCount(count);
		} else {
			result.setState(StockSpecialDayOperationStateEnum.INNER_ERROR.getState());
		}
		return result;
	}

	@Override
	@Transactional
	public StockSpecialDayExecution add(StockSpecialDay stockSpecialDay) throws StockSpecialDayOperationException {
		// 空值判断
		if (stockSpecialDay == null) {
			return new StockSpecialDayExecution(StockSpecialDayOperationStateEnum.NULL);
		} 
		int effectedNum;
		try {
			stockSpecialDay.setCreateTime(new Date());
			stockSpecialDay.setLastEditTime(new Date());
			// 执行插入数据操作 
			effectedNum = stockSpecialDayDao.insert(stockSpecialDay);
			if (effectedNum <= 0) {
				throw new StockSpecialDayOperationException("StockSpecialDay信息插入失败");
			}
		} catch (Exception e) {
			throw new StockSpecialDayOperationException("Add stockSpecialDay error:" + e.getMessage());
		}
		return new StockSpecialDayExecution(StockSpecialDayOperationStateEnum.SUCCESS, stockSpecialDay);
	}

	@Override
	@Transactional
	public StockSpecialDayExecution modify(StockSpecialDay stockSpecialDay) throws StockSpecialDayOperationException {
		// 空值判断
		if (stockSpecialDay == null) {
			return new StockSpecialDayExecution(StockSpecialDayOperationStateEnum.NULL);
		} 
		int effectedNum;
		try {
			// 把记录的最后更新时间设置为当前时间 
			stockSpecialDay.setLastEditTime(new Date());
			// 执行修改更新操作 
			effectedNum = stockSpecialDayDao.update(stockSpecialDay);
			if (effectedNum <= 0) {
				throw new StockSpecialDayOperationException("StockSpecialDay信息更新失败");
			}
		} catch (Exception e) {
			throw new StockSpecialDayOperationException("Update stockSpecialDay error:" + e.getMessage());
		}
		return new StockSpecialDayExecution(StockSpecialDayOperationStateEnum.SUCCESS, stockSpecialDay);
	}

	@Override
	@Transactional
	public StockSpecialDayExecution remove(Long recordID) throws StockSpecialDayOperationException{
		// 空值判断
		if (recordID == null) {
			return new StockSpecialDayExecution(StockSpecialDayOperationStateEnum.NULL);
		} 
		int effectedNum;
		try {
			// 执行删除操作 
			effectedNum = stockSpecialDayDao.delete(recordID);
			if (effectedNum <= 0) {
				throw new StockSpecialDayOperationException("StockSpecialDay单条信息删除失败");
			}
		} catch (Exception e) {
			throw new StockSpecialDayOperationException("Delete stockSpecialDay error:" + e.getMessage());
		}
		return new StockSpecialDayExecution(StockSpecialDayOperationStateEnum.SUCCESS);
	}

	@Override
	@Transactional
	public StockSpecialDayExecution remove(StockSpecialDay stockSpecialDay) throws StockSpecialDayOperationException{
		// 空值判断
		if (stockSpecialDay == null) {
			return new StockSpecialDayExecution(StockSpecialDayOperationStateEnum.NULL);
		} 
		int effectedNum;
		try {
			// 执行删除操作 
			effectedNum = stockSpecialDayDao.delete(stockSpecialDay.getRecordID());
			if (effectedNum <= 0) {
				throw new StockSpecialDayOperationException("StockSpecialDay单条信息删除失败");
			}
		} catch (Exception e) {
			throw new StockSpecialDayOperationException("Delete stockSpecialDay error:" + e.getMessage());
		}
		return new StockSpecialDayExecution(StockSpecialDayOperationStateEnum.SUCCESS, stockSpecialDay);
	}

	@Override
	@Transactional
	public StockSpecialDayExecution removeBatchByID(List<Long> recordIDList) throws StockSpecialDayOperationException{
		// 空值判断
		if (recordIDList == null || recordIDList.size() < 1) {
			return new StockSpecialDayExecution(StockSpecialDayOperationStateEnum.NULL);
		} 
		int effectedNum;
		try {
			// 执行删除操作 
			effectedNum = stockSpecialDayDao.deleteBatch(recordIDList);
			if (effectedNum <= 0) {
				throw new StockSpecialDayOperationException("StockSpecialDay多条信息批量删除失败");
			}
		} catch (Exception e) {
			throw new StockSpecialDayOperationException("Batch Delete stockSpecialDay error:" + e.getMessage());
		}
		StockSpecialDayExecution exe = new StockSpecialDayExecution(StockSpecialDayOperationStateEnum.SUCCESS);
		exe.setCount(effectedNum);
		return exe;
	}

	@Override
	@Transactional
	public StockSpecialDayExecution removeBatch(List<StockSpecialDay> stockSpecialDayList) throws StockSpecialDayOperationException{
		// 空值判断
		if (stockSpecialDayList == null) {
			return new StockSpecialDayExecution(StockSpecialDayOperationStateEnum.NULL);
		} 
		List<Long> recordIDList = new ArrayList<Long>();
		for (int i=0; i<stockSpecialDayList.size(); i++) {
			recordIDList.add(stockSpecialDayList.get(i).getRecordID());			
		}
		int effectedNum;
		try {
			// 执行删除操作 
			effectedNum = stockSpecialDayDao.deleteBatch(recordIDList);
			if (effectedNum <= 0) {
				throw new StockSpecialDayOperationException("StockSpecialDay多条信息批量删除失败");
			}
		} catch (Exception e) {
			throw new StockSpecialDayOperationException("Batch Delete stockSpecialDay error:" + e.getMessage());
		}
		StockSpecialDayExecution exe = new StockSpecialDayExecution(StockSpecialDayOperationStateEnum.SUCCESS);
		exe.setCount(effectedNum);
		return exe;
	}

}
